using UnityEngine;
using System.Collections;

namespace ViNoToolkit{

/// <summary>
/// Sample TriggerVolume event receiver.
/// </summary>
	public class TriggerVolumeEventReceiver : MonoBehaviour {
	[System.Serializable]
	public class TriggerVolEventEntry{
		public ViNode node;
		public GameObject triggerVolume;
	}

	public ScenarioNode scenario;
	public TriggerVolEventEntry[] events;

	private bool m_LockTrigger;
	
	void OnTriggerEvent( ViNoEventData data ){
		TriggerVolumeEventData tdata = data as TriggerVolumeEventData;
		if (tdata == null) {
			return;
		}

		// If event is already triggered, return.
		if( ! m_LockTrigger ){
			m_LockTrigger = true;
		}
		else{
			return;
		}

		Collider col = tdata.collider;

		Debug.Log( "OnTriggerEvent" );

		foreach( TriggerVolEventEntry ent in events ){
			if( col.gameObject.name.Equals( ent.triggerVolume.name ) ){
				Debug.Log ( "Play from : " + ent.node.GetNodeLabel() );
				scenario.PlayFrom( ent.node.GetNodeLabel() );
				break;
			}
		}
	}

	/// <summary>
	/// Raises the finish event event.
	/// </summary>
	void OnFinishEvent(){
		Debug.Log( "OnFinishEvent" );
		m_LockTrigger = false;
	}
	
	void Start () {
		scenario.CompileAndSetCode();
	}

}

}
